# ======================================================================
#
# Copyright (C) ADN
# ADN::LIBRARY::ADAPTERS::Execute Execute.pm
# Description:
#  - Gestion des types
#
# ======================================================================

package ADN::LIBRARY::ADAPTERS::Execute;

use strict;
use vars qw($AUTOLOAD);
use ADN::XML::XMLtoOBJ;
use ADN::CONF::Read;
use ADN::LIBRARY::ADAPTERS::CISCO::IOS::Ios;

# #######################
# CREATE OBJECT
# #######################
sub new
{
	# Construction de l'objet $self
	my $self = shift;
	my $class = ref($self) || $self;
	return $self if ref $self;
	$self = bless {} => $class;
	$self->{error} = 0;
  $self->_init();
	return $self;
}

# #######################
# Fonction initialisation
# #######################
sub _init
{
	my $self = shift;
  # Chargement du fichier de configuration XML + Initialisation variable.
  my $conf = ADN::CONF::Read->new();
  ($conf =~ m/^[-]1$/smx) ? ($self->{error} = 1) : ();
  if ($self->{error} != 1)
  {
    my %type_equip = $conf->type_equip();
    ($conf->type_equip() =~ m/^[-]1$/smx) ? ($self->{error} = 1) : ();
	  $self->{type_path} = $type_equip{type_path};
	  $self->{type_file} = $type_equip{type_file};
	  $self->{path_inc} = $conf->_send_path();
	  undef $conf;
  }
  return;
}

# #######################
# READ FILE
# #######################
sub file_xml
{
	my $self = shift;
	my ($balise) = @_;
  my $file = $self->{path_inc}.$self->{type_path}.$self->{type_file};

  if (-e $file && -f $file && -r $file)
  {
	  if(open FILE, '<', $file)
	  {
		  while ( my $l1 = <FILE>)
		  {
			  $self->{xml} .= $l1;
		  }
		  if (!close FILE) {}
	  }
	}
  return ($self->Division_XML($balise));
}

# #######################
# Establishment XML
# #######################
sub Division_XML
{
	my $self = shift;
	my ($balise) = @_;

	if ($self->{xml} =~ m/\<$balise\>/smx)
	{
    $self->{xml} =~ s/\s|\n|\r//gsmx;
    $self->{xml} =~ s/^.*\<$balise\>(.*)\<\/$balise\>.*$/$1/gsmx;
    if ($self->{xml} =~ m/^[\w\d\_\:]*$/smx)
    {
      return ($self->{xml});
    }
  }
  $self->{error} = 1;
  return;
}


# #######################
# DESTROY
# #######################
sub DESTROY
{
  my $self = shift;
  undef $self;
  return;
}

# #######################
# FUNCTION ERROR
# #######################
sub error
{
  my $self = shift;

  if ($self->{error} != 0)
  {
    if ($self->{error} == 1)
    {
      return (-1);
    }
  }
}

# #######################
# AUTOLOAD
# #######################
sub AUTOLOAD
{
  my $self = shift;
  my $balise = $AUTOLOAD;
  $balise =~ s/.*:://smx;
  my ($balise2, $function, $connexion, $credential, $protocol, $string ) = @_;
  my ($xml, $send);

  if (defined($balise) and defined($balise2))
  {
    if ($balise =~ m/^[a-zA-Z0-9\_]+$/smx)
    {
      if (defined($self->{path_inc}) and defined($self->{type_path}) and defined($self->{type_file}))
      {
        $xml =  ADN::XML::XMLtoOBJ->new($self->{path_inc}.$self->{type_path}.$self->{type_file});
        ($xml =~ m/^[-]1$/smx) ? ($self->{error} = 1, return ($self->error())) : (); # Controle de sortie du module XMLtoOBJ
        (($xml->$balise()) =~ m/^[-]1$/smx) ? ($self->{error} = 1) : ();
        (($xml->$balise2()) =~ m/^[-]1$/smx) ? ($self->{error} = 1) : ();
        $xml = undef;
        $xml = $self->file_xml($balise2);
        ($self->{error} != 0) ? (return ($self->error())) : ();
        return ($xml->$function($connexion, $credential, $protocol, $string)) if (defined $string);
        return ($xml->$function($connexion, $credential, $protocol));
      }
      else
      {
        $self->{error} = 1;
        return ($self->error());
      }
    }
  }
}
1;
__END__
